/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.entity.Users;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.qdu.dao.UsersDao;


/**
 * StudentDaoImpl类，StudentDao接口的实现类，包含了Student表相关数据库操作的实现代码
 *
 * @author Lenovo
 */
public class UsersDaoImpl extends BaseDaoImpl implements UsersDao {

    @Override
    public int insert(Users u) {
        return executeUpdate("insert into Users values(?,?,?,?,?,?,?,?,?,?,?)", u.getUno(), u.getUname(), u.getUpwd(), u.getUquestion(), u.getUanswer(), u.getUid(), u.getUgender(), u.getUphone(), u.getUcity(), u.getUocp(), u.getUemail()); 
    }

    @Override
    public int update(Users u) {
        return executeUpdate("update Users set Uname=?,Uid=?,Ugender=?,Uphone=?,Ucity=?,Uocp=?,Uemail=? where Uno=?", u.getUname(), u.getUid(), u.getUgender(), u.getUphone(), u.getUcity(), u.getUocp(), u.getUemail(),u.getUno()); 
    }

    @Override
    public int deleteByUno(int Uno) {
        return executeUpdate("delete Users where Uno=?", Uno);
    }

    @Override
    public Users loginUsers(int Uno,String Upwd) {
         Connection con = null;
        PreparedStatement ps = null;
        PreparedStatement ss1 = null;
        PreparedStatement ss2 = null;
        ResultSet rs = null;
        ResultSet ss3 = null;
        ResultSet ss4 = null;
        Users s = null;
        Users n=null;
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Users where Uno=?");//预编译
            ps.setInt(1, Uno);//占位符，向第一个位置赋值
            rs = ps.executeQuery();//执行
            while (rs.next()) {
                s=new Users(rs.getInt(1),rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9),rs.getString(10), rs.getString(11));
            }
            ss1=con.prepareStatement("select Uno from Users where Uno='"+Uno+"'");
            ss2=con.prepareStatement("select Upwd from Users where Upwd='"+Upwd+"'");
            ss3=ss1.executeQuery();
            ss4=ss2.executeQuery();
            if(ss3.next()&&ss4.next())
            {
                s=s;
            }
            else{
                s=n;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        
       return s;
    }

    @Override
    public List<Users> getAll() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Users> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Users");
            rs = ps.executeQuery();
            while(rs.next()){
                list.add(new Users(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(null, ps, con);
        }
        return list;
    }
    
    public int updatepwd(Users u) {
        return executeUpdate("update Users set Upwd=? where Uanswer=?", u.getUpwd(),u.getUanswer()); 
    }

    @Override
    public Users getOneByUno(int Uno) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Users where Uno=?");
            ps.setInt(1, Uno);
            rs = ps.executeQuery();
            while (rs.next()) {
               return new Users(rs.getInt(1),rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9),rs.getString(10), rs.getString(11));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        
       return null;
    }


}
